package com.wc.alorithm_blue_bridge._模拟.逆序对期望;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/13 15:30
 * @description 逆序对期望
 * 题意
 * 有一个数组，包含 1 到 n 这 n 个整数，初始为一个从小到大的有序排列:
 * {1, 2, 3, 4, · · · , n} 。一次随机交换操作指：均匀随机选取两个位置 i, j ∈ [1, n] 且
 * i ≠ j ，然后交换数组中这两个位置上的数。那么对于 n = 51 ，对初始数组进行
 * 两次随机交换操作之后，数组中的逆序对的数量的期望是多少个。
 */
public class Main {
    public static void main(String[] args) {
        long sum = 0;
        int n = 51;
        for (int a = 1; a <= n; a++) {
            for (int b = a + 1; b <= n; b++) {
                for (int c = 1; c <= n; c++) {
                    for (int d = c + 1; d <= n; d++) {
                        // 全部不相等
                        if (a != c && a != d && b != c && b != d) {
                            // c < d < a < b
                            if (d < a) sum += 2;
                                // c < a < d < b
                            else if (c < a && a < d && d < b) sum += 4;
                                // c < a < b < d
                            else if (c < a && b < d) sum += 6;
                                // a < c < d < b
                            else if (a < c && c < d && d < b) sum += 6;
                                // a < c < b < d
                            else if (a < c && c < b && b < d) sum += 4;
                                // a < b < c < d
                            else if (b < c) sum += 2;
                        } else if (b == c || a == d) {
                            sum += 2;
                        } else if (b == d && a != c || (a == c && b != d)) {
                            sum += 2;
                        } else {
                            sum += 0;
                        }
                    }
                }
            }
        }
        double res = 1.0 * sum * 4 / (50 * 51 * 50 * 51);
        System.out.printf("%.2f", res);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
